
	local TEXT_TYPE_NONE = 1
	local TEXT_TYPE_COLOR = 2
	local TEXT_TYPE_EMOTE = 3
	
	WWCHAT = {}
	WWCHAT.Lines = {}
	WWCHAT.ChatOpen = false
	WWCHAT.ChatEntry = nil
	WWCHAT.ChatFun = nil
	WWCHAT.ChatNotif = nil
	nextLower = 0
	alpha = 255
	stopAlpha = false
	NotifText = "WW"
	
	local SCRH = ScrH()
	local SCRW = ScrW()

		local w, h = surface.GetTextSize( NotifText )
		
		WWCHAT.ChatEntry = vgui.Create( "DTextEntry" )
		WWCHAT.ChatEntry:SetPos( 35, SCRH - 310 )
		WWCHAT.ChatEntry:SetSize( 500, 20 )
		WWCHAT.ChatEntry:SetVisible( false )
		WWCHAT.ChatEntry:SetEditable( false )
		
		WWCHAT.ChatFun = vgui.Create( "DPanel" )
		WWCHAT.ChatFun:SetPos(35, SCRH - 310)
		WWCHAT.ChatFun:SetSize( 0, 0 )
		WWCHAT.ChatFun:SetVisible( false )
		WWCHAT.ChatFun.Paint = function()
			surface.SetDrawColor( 0, 0, 0, 100 )
			surface.DrawRect( 0, 0, WWCHAT.ChatFun:GetWide(), WWCHAT.ChatFun:GetTall() )
		end

	surface.CreateFont( "Bangle Thin", 24, 400, true, false, "ChatText" )
	function WWCHAT.OpenChatEntry()
		WWCHAT.ChatOpen = true
		WWCHAT.ChatEntry:SetVisible( true )
		WWCHAT.ChatFun:SetVisible( true )
		gui.EnableScreenClicker( true )
		NotifText = "WW"
		WWCHAT.UpdateChatEntry("")
		return true
	end
	hook.Add( "StartChat", "WWCHATOpenChat", WWCHAT.OpenChatEntry )

	function WWCHAT.CloseChatEntry()
		WWCHAT.ChatOpen = false
		WWCHAT.ChatEntry:SetVisible( false )
		WWCHAT.ChatFun:SetVisible( false )
		gui.EnableScreenClicker( false )
	end
	hook.Add( "FinishChat", "WWCHATCloseChat", WWCHAT.CloseChatEntry )

	function WWCHAT.UpdateChatEntry( text )
			
			surface.SetFont( "ChatText" )
			local w, h = surface.GetTextSize( NotifText )

			local RetText = SpellCheck( text )
				
			if w then twa = w else twa = w end
			
			WWCHAT.ChatFun:SetPos(35, SCRH - 310)
			WWCHAT.ChatFun:SetSize( twa, 26 )
			
			WWCHAT.ChatEntry:SetPos( 35 + twa + 5, SCRH - 310 )
			WWCHAT.ChatEntry:SetSize( 500 - twa, 20 )
		
			WWCHAT.ChatEntry:SetText( RetText )
			
			
			for k,v in pairs(WW.Chat.Status) do
				if string.find(string.sub(text, 1, v.Length), v.Command) then
					if w then twa = w + 10 else twa = w end
					
					WWCHAT.ChatFun:SetPos(30, SCRH - 310)
					WWCHAT.ChatFun:SetSize( twa + 5, 26 )
					
					NotifText = v.Name
					
					WWCHAT.UpdateChatEntry(string.Replace(string.sub(text, 1, v.Length), v.Command, "") .. string.sub(text, v.Length + 1, string.len(text)))
				end
			end
	end
	hook.Add( "ChatTextChanged", "WWCHATUpdateChatEntry", WWCHAT.UpdateChatEntry )

	function WWCHAT.PlyChatMessage( ply, msg, pteam, dead )
		col = team.GetColor( ply:Team() )
		WWCHAT.ParseLine( ply:Nick() .. ": " .. msg, col )
	end
	hook.Add( "OnPlayerChat", "WWCHATChatMessage", WWCHAT.PlyChatMessage )
	
	function WWCHAT.EventMessage( ply, plynick, msg, mtype )
		local col = Color( 255, 255, 255, 255 )
		
		for k,v in pairs(player.GetAll()) do
			if string.find(string.sub(msg, 1, string.len(v:Nick())), v:Nick()) then
				col = team.GetColor(v:Team())
			end
		end
		
		WWCHAT.ParseLine( msg, col )
	end
	hook.Add( "ChatText", "WWCHATEventMessage", WWCHAT.EventMessage )
	
	function WWCHAT.ParseLine( line, color )
		surface.SetFont( "ChatText" )
		local lineValues = {}
		lineValues.Type = {}
		lineValues.Value = {}
		lineValues.Text = {}
		lineValues.Width = {}
		lineValues.Length = 0
		alpha = 255
		local toParse = line
		local id = 1
		while toParse != "" do
				local w, h = surface.GetTextSize( toParse )
				table.insert( lineValues.Type, id, TEXT_TYPE_COLOR )
				table.insert( lineValues.Value, id, color )
				table.insert( lineValues.Text, id, toParse )
				table.insert( lineValues.Width, id, w )
				toParse = ""
				id = id + 1
		end
		lineValues.Length = id
		table.insert( WWCHAT.Lines, lineValues )
	end
	
	alpha = 255
	
	function WWCHAT.DrawLine( x, y, line, alpha )
		local curX = x
		local curY = y
		local num = line.Length
		
			for i = 1, line.Length do
				local t = line.Type[i]
				local w = line.Width[i]
				local val = line.Value[i]
				local text = line.Text[i]
				
				colaa = Color(0, 0, 0, alpha / 2)
				
				if t == TEXT_TYPE_COLOR then colaa = Color( val.r, val.g, val.b, alpha / 2 ) end
				
				if w then tw = w + 20 else tw = w end
				
				draw.RoundedBox( 0, curX - 10, curY, tw, 26, colaa )
				
				if t == TEXT_TYPE_NONE then
					draw.DrawText( text, "ChatText", curX, curY, Color(255, 255, 255, alpha), 0)
				elseif t == TEXT_TYPE_COLOR then
					draw.DrawText( text, "ChatText", curX, curY, Color(255, 255, 255, alpha), 0)
				end
				if w then curX = curX + w or curX end
			end
	end	

	function WWCHAT.DrawChat()
		
		local SCRH = ScrH()
		
		surface.SetFont( "ChatText" )
		if WWCHAT.ChatOpen then
			
			--surface.SetDrawColor( Color( 0, 0, 0, 100 ) )
			--surface.DrawRect( 35, SCRH - 450, 500, 130 )
			
			alpha = 255
			draw.DrawText(NotifText, "ChatText", 35, SCRH - 310, Color( 255, 255, 255, 255), 0)
		end
		
		local _, lineHeight = surface.GetTextSize( "H" )
		local curX = 70
		local curY = SCRH - 348
		
		alpha = math.Approach(alpha, 0, FrameTime() * 20 )
		
		for i = 0, 4 do
			local line = WWCHAT.Lines[#WWCHAT.Lines-i]
			if line then
				curX = 70
				WWCHAT.DrawLine( curX, curY, line, alpha )
				curY = curY - lineHeight - 2
			end
		end
	end

	
	hook.Add( "HUDPaint", "WWCHATDRAWLINE", WWCHAT.DrawChat )
	